package xcmg.device.web.safetyStock;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.dao.entity.safetyStock.ComSafetyStockInfo;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.safetyStock.ComSafetyStockInfoService;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;

import java.util.Date;

/**
 * @author wangyong
 */
@RestController
@RequestMapping("/comSafetyStock")
public class ComSafetyStockInfoController extends BasicController {

    @Autowired
    private ComSafetyStockInfoService comSafetyStockInfoService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult save(@RequestBody ComSafetyStockInfo comSafetyStockInfo) {
        Integer count = null;
        try {
            comSafetyStockInfo.setOrgId(getOrgId());
            comSafetyStockInfo.setCreateId(getUserId());
            comSafetyStockInfo.setCreateName(getUserName());
            comSafetyStockInfo.setCreateTime(new Date());
            comSafetyStockInfo.setUpdateId(getUserId());
            comSafetyStockInfo.setUpdateName(getUserName());
            comSafetyStockInfo.setUpdateTime(new Date());
            count = comSafetyStockInfoService.save(comSafetyStockInfo);
        } catch (Exception e) {
            return actionResult(ErrorCode.Failure, e.getMessage());
        }
        return actionValueResult(count);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult update(@RequestBody ComSafetyStockInfo comSafetyStockInfo) {
        Integer count = null;
        try {
            comSafetyStockInfo.setOrgId(getOrgId());
            comSafetyStockInfo.setUpdateId(getUserId());
            comSafetyStockInfo.setUpdateName(getUserName());
            comSafetyStockInfo.setUpdateTime(new Date());
            count = comSafetyStockInfoService.update(comSafetyStockInfo);
        } catch (Exception e) {
            return actionResult(ErrorCode.Failure, e.getMessage());
        }
        return actionValueResult(count);
    }

    @RequestMapping(value = "/item", method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult item()throws Exception{
        return actionValueResult(comSafetyStockInfoService.item(getOrgId()));
    }
}
